//
//  PPTemplateTableViewCell.m
//  pickpic
//
//  Created by 胡文广 on 2017/3/25.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPTemplateTableViewCell.h"
#import "PPTemplateModel.h"
#import "UAProgressView.h"
@interface PPTemplateTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *desc;

@end
@implementation PPTemplateTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.progressV.tintColor = [UIColor blackColor];
    self.progressV.borderWidth = 0.8;
    self.progressV.lineWidth = 3.0f;
    self.progressV.fillOnTouch = NO;
    UIImageView *centerImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    centerImg.image = [UIImage imageNamed:@"downArr"];
    
    
    
    self.progressV.centralView = centerImg;
}

- (void)setTemplateM:(PPTemplateModel *)templateM{

    _templateM = templateM;
    
    [self.pic sd_setImageWithURL:[NSURL URLWithString:templateM.url1] placeholderImage:[UIImage imageNamed:@"light-off"]];
    self.pic.contentMode = UIViewContentModeScaleAspectFit;
    
    self.title.text = templateM.title;
    self.desc.text = templateM.describe;
    
    [self updateProgress:_templateM];
    
}

- (void)updateProgress:(PPTemplateModel *)template{
    
    [(UIImageView *)self.progressV.centralView setBackgroundColor:WHITE];
     self.progressV.backgroundColor = CLEAR;
    [(UIImageView *)self.progressV.centralView setImage:[UIImage imageNamed:@""]];

    if (template.downloadType == PPTemplateDownLoadTypeNot) {
       [(UIImageView *)self.progressV.centralView setImage:[UIImage imageNamed:@"downArr"]];
        [self.progressV setProgress:0];
        
    }else if (template.downloadType == PPTemplateDownLoadTypeBeing){
        [(UIImageView *)self.progressV.centralView setBackgroundColor:BLACK];
        [self.progressV setProgress:((int)((template.receivedLength * 100.0f) + 1.01) % 100) / 100.0f];
    }else if (template.downloadType == PPTemplateDownLoadTypeFinish){
        [(UIImageView *)self.progressV.centralView setBackgroundColor:WHITE];
        [(UIImageView *)self.progressV.centralView setImage:[UIImage imageNamed:@"loadFinsh"]];
        [self.progressV setProgress:0];
    }else{

        [(UIImageView *)self.progressV.centralView setBackgroundColor:CLEAR];
        self.progressV.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"finshload"]];
        [self.progressV setProgress:0];
    }

}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated
//{
//    // 获取 contentView 所有子控件
//    NSArray<__kindof UIView *> *subViews = self.contentView.subviews;
//    // 创建颜色数组
//    NSMutableArray *colors = [NSMutableArray array];
//    
//    for (UIView *view in subViews) {
//        // 获取所有子控件颜色
//        [colors addObject:view.backgroundColor ?: [UIColor clearColor]];
//    }
//    // 调用super
//    [super setSelected:selected animated:animated];
//    // 修改控件颜色
//    for (int i = 0; i < subViews.count; i++) {
//        subViews[i].backgroundColor = colors[i];
//    }
//}
//
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    // 获取 contentView 所有子控件
    NSArray<__kindof UIView *> *subViews = self.contentView.subviews;
    // 创建颜色数组
    NSMutableArray *colors = [NSMutableArray array];
    
    for (UIView *view in subViews) {
        // 获取所有子控件颜色
        [colors addObject:view.backgroundColor ?: [UIColor clearColor]];
    }
    // 调用super
    [super setHighlighted:highlighted animated:animated];
    // 修改控件颜色
    for (int i = 0; i < subViews.count; i++) {
        subViews[i].backgroundColor = colors[i];
    }
}
@end
